#pragma once
#include<vector>
#include<iostream>
namespace bit
{
    template<class T>
    class my_less
    {
    public:
        bool operator()(const T& T1, const T& T2)
        {
            return T1 < T2;
        }
    };

    template<class T>
    class my_grater
    {
    public:
        bool operator()(const T& T1, const T& T2)
        {
            return T1 > T2;
        }
    };

    template <class T, class Container = std::vector<T>, class Compare = my_less<T> >
    class priority_queue
    {
    public:
        priority_queue()
        {}

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                _c.push_back(*first);
                ++first;
            }
            for (int i = (_c.size() - 1 - 1) / 2; i >= 0; --i)
            {
                AdjustDown(i);
            }
        }

        bool empty() const
        {
            return _c.empty();
        }

        size_t size() const
        {
            return _c.size();
        }

        const T& top() const
        {
            return _c.front();
        }

        void push(const T& x)
        {
            _c.push_back();
            AdjustUp(_c.size() - 1);
        }

        void pop()
        {
            std::swap(_c[0], _c[_c.size() - 1]);
            _c.pop_back();
            AdjustDown(0);
        }

        void AdjustUp(size_t child)
        {
            size_t parent = (child - 1) / 2;
            while (child < 0)
            {
                if (comp(_c[parent], _c[child]))
                {
                    std::swap(_c[parent], _c[child]);
                }
                child = parent;
                parent = (child - 1) / 2;
            }
        }

        void AdjustDown(size_t parent)
        {
            size_t child = parent * 2 + 1;
            
            while (child < _c.size())
            {
                if (child + 1 < _c.size() && comp(_c[child], _c[child + 1]) )
                {
                    child++;
                }
                if (comp(_c[parent], _c[child]))
                {
                    std::swap(_c[parent], _c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

    private:
        Container _c;
        Compare comp;
    };
};
